package com.lsy.android.pingpang;

import com.lsy.android.pingpang.bluetooth.BluetoothManager;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class BluetoothActivity extends Activity {

	private Button mBtnPlayPreper;
	private TextView mTxtPlayPreper;

	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.bluetooth_activity);

		mBtnPlayPreper = (Button) findViewById(R.id.btn_play_preper);
		mBtnPlayPreper.setVisibility(View.INVISIBLE);
		mTxtPlayPreper = (TextView) findViewById(R.id.txt_play_preper);
		mTxtPlayPreper.setVisibility(View.INVISIBLE);
		final CheckBox btnServer = (CheckBox) findViewById(R.id.btn_server);
		ImageView imgServer = (ImageView) findViewById(R.id.img_server);
		final CheckBox btnClient = (CheckBox) findViewById(R.id.btn_client);
		ImageView imgClient = (ImageView) findViewById(R.id.img_client);

		btnServer.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					btnServer.setBackgroundResource(R.drawable.btn_checked);
					btnClient.setChecked(false);
					mBtnPlayPreper.setVisibility(View.VISIBLE);
					mBtnPlayPreper
							.setBackgroundResource(R.drawable.btn_preper1);
				} else {
					btnServer.setBackgroundResource(R.drawable.btn_unchecked);
				}
			}
		});
		btnClient.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					btnClient.setBackgroundResource(R.drawable.btn_checked);
					btnServer.setChecked(false);
					mBtnPlayPreper.setVisibility(View.VISIBLE);
					mBtnPlayPreper
							.setBackgroundResource(R.drawable.btn_preper2);
				} else {
					btnClient.setBackgroundResource(R.drawable.btn_unchecked);
				}
			}
		});
		imgServer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!btnServer.isChecked()) {
					btnServer.setChecked(true);
				}
			}
		});
		imgClient.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!btnClient.isChecked()) {
					btnClient.setChecked(true);
				}
			}
		});

		mBtnPlayPreper.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent intent = new Intent(BluetoothActivity.this,
						BluetoothGameActivity.class);
				intent.putExtra("is_server", btnServer.isChecked());
				startActivity(intent);
			}
		});

		if (BluetoothAdapter.getDefaultAdapter() == null) {
			Toast.makeText(this, "Bluetooth is not available",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
			Intent enableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivity(enableIntent);
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onDestroy() {
		BluetoothManager.close();
		super.onDestroy();
	}

}
